package com.changgou.service.goods.controller;

import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RequestMapping("/brand")
@RestController
public class BrandController {

    @Resource
    private BrandService brandService;

    //查询品牌列表
    @GetMapping
    public Result<List<Brand>> findList() {
        List<Brand> brandList = brandService.findList();
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable("id") Integer id) {
        Brand brand = brandService.getById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", brand);
    }

    /***
     * 新增品牌
     * @param brand
     */
    @PostMapping
    public Result add(@RequestBody Brand brand) {
        brandService.save(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 修改品牌数据
     * @param brand
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody Brand brand) {
        brandService.updateByIds(id, brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 删除品牌
     * @param id
     */
    @DeleteMapping("/{id}")
    public Result delById(@PathVariable("id") Integer id) {
        brandService.removeById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping("/search")
    public Result<List<Brand>> search(@RequestParam Map<String, Object> searchMap) {
        List<Brand> list = brandService.searchMap(searchMap);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 分页搜索实现
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@RequestParam(value = "page") Integer page, @RequestParam(value = "size") Integer size) {
        PageResult pageResult = brandService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }
//
//    /***
//     * 分页搜索实现
//     * @param searchMap
//     * @param page
//     * @param size
//     * @return
//     */
//    @GetMapping("/searchPage/{page}/{size}")
//    public Result findPage(@RequestParam Map searchMap, @PathVariable("page")int page, @PathVariable("size") int size){
//        int i=1/0;
//        Page pageInfo = brandService.findPage(searchMap, page, size);
//        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getResult());
//        return new Result(true,StatusCode.OK,"查询成功",pageResult);
//    }
}
